package cn.flying.cloud.system.role.service.impl;

import javax.annotation.Resource;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.common.enums.SeqTypeEnum;
import cn.flying.cloud.common.enums.StatusEnum;
import cn.flying.cloud.system.log.service.SysSeqInfoService;
import cn.flying.cloud.system.role.entity.SysRoleInfo;
import cn.flying.cloud.system.role.entity.SysRoleRes;
import cn.flying.cloud.system.role.mapper.SysRoleInfoMapper;
import cn.flying.cloud.system.role.service.SysRoleInfoService;
import cn.flying.cloud.system.role.service.SysRoleResService;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.entity.SysUserRole;
import cn.flying.cloud.system.user.service.SysUserRoleService;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-02 13:07:31
 */
@Service
public class SysRoleInfoServiceImpl extends ServiceImpl<SysRoleInfoMapper, SysRoleInfo> implements SysRoleInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysSeqInfoService sysSeqInfoService;
    @Resource
    private SysRoleResService sysRoleResService;
    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询角色信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysRoleInfo> getPageList(PageRequest<SysRoleInfo> request) {
        SysRoleInfo param = request.getParam();
        LambdaQueryWrapper<SysRoleInfo> wrapper = getLambdaQueryWrapper(param);
        Page<SysRoleInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysRoleInfo> getLambdaQueryWrapper(SysRoleInfo param) {
        LambdaQueryWrapper<SysRoleInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getRoleName())) {
                wrapper.like(SysRoleInfo::getRoleName, "%" + param.getRoleName() + "%");
            }
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(SysRoleInfo::getStatus, param.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @Override
    public List<SysRoleInfo> getList(SysRoleInfo sysRoleInfo) {
        LambdaQueryWrapper<SysRoleInfo> wrapper = getLambdaQueryWrapper(sysRoleInfo);
        return this.list(wrapper);
    }

    /**
     * 查询角色信息表详情数据
     *
     * @param sysRoleInfo
     * @return
     */
    @Override
    public SysRoleInfo getOneObj(SysRoleInfo sysRoleInfo) {
        LambdaQueryWrapper<SysRoleInfo> wrapper = new LambdaQueryWrapper<>();
        return this.getOne(wrapper);
    }


    /**
     * 新增角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysRoleInfo sysRoleInfo) {
        sysRoleInfo.setRoleId(sysSeqInfoService.getNextSeq(SeqTypeEnum.ROLE.name()));
        sysRoleInfo.setTenantId(Ft.Local.TenantId());
        return this.save(sysRoleInfo);
    }

    /**
     * 更新角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysRoleInfo sysRoleInfo) {
        return this.saveOrUpdate(sysRoleInfo);
    }

    /**
     * 删除角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysRoleInfo sysRoleInfo) {
        LambdaQueryWrapper<SysRoleInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleInfo::getRoleId, sysRoleInfo.getRoleId());
        boolean removed = this.remove(wrapper);

        // 同时删除用户角色信息
        LambdaQueryWrapper<SysUserRole> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysUserRole::getRoleId, sysRoleInfo.getRoleId());
        sysUserRoleService.remove(wrapper1);

        // 同时删除角色资源信息
        LambdaQueryWrapper<SysRoleRes> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SysRoleRes::getRoleId, sysRoleInfo.getRoleId());
        sysRoleResService.remove(wrapper2);

        return removed;
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }

    /**
     * 获取所有的角色信息表数据
     *
     * @return
     */
    @Override
    public List<SysRoleInfo> getAllRoleList() {
        LambdaQueryWrapper<SysRoleInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleInfo::getStatus, StatusEnum.STATUS_Y.getCode());
        return this.list(wrapper);
    }

    /**
     * 查询角色对应的用户
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysUserInfo> getUserByRoleId(String roleId) {
        return this.getBaseMapper().getUserByRoleId(Ft.Local.TenantId(), roleId);
    }
}
